// export const domain = 'http://152.136.235.121:8085';
// export const domain = 'https://slzxbak.bczhhy.com:8085';
export const domain = 'https://zhln.bczhhy.com:8085'

export const http = (method = "GET", cmd, data = {}) => {
  const innerData = {};
  let Cookies = '';

  if (cmd != '/login') {
    Cookies = wx.getStorageSync('token');
  }
  return new Promise((resolve, reject) => {
    wx.request({
      method,
      url: `${domain}${cmd}`,
      data: {
        ...innerData,
        ...data
      },
      header: {
        Cookie: Cookies
      },
      success(res) {
        const {
          data,
          statusCode,
          cookies
        } = res;
        if (statusCode == 200 && data.code === 'SUCCESS') {
          if (cookies.length) {
            wx.setStorageSync('token', cookies[0]?.split(';')[0])
          }
          resolve(data.data);
        } else {
          wx.showToast({
            title: data.message,
            icon: 'error'
          })
          if (data.message.includes('重新登录')) {
            setTimeout(() => {
              wx.navigateTo({
                url: '/pages/auth/accountLogin/accountLogin',
              });
            }, 1000);
            return;
          }
          reject(data.data)
        }
      },
      fail(err) {
        console.log('request fail: ', err);
        reject(err)
      }
    })
  })
}